<template>
  <div class="wrap">
    <el-dialog
      :title="titleHead"
      :visible.sync="dialogVisible"
      width="65%"
      :showClose="false"
    >
      <!-- <span>这是一段信息</span> -->
      <span class="cancel" @click="goodsCancel" style="cursor:pointer;">X</span>
      <div class="goods-wrap">
        <div class="headInfo">
          <el-form
          :inline="true"
          :model="dataForm"
          @keyup.enter.native="getDataList()"
        >
        <el-form-item class="date">
          <div>单据日期:</div>
            <el-date-picker
              v-model="dataForm.barcode"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
        </el-form-item>
        <el-form-item class="inp">
            <div>单据编号：</div>
            <el-input
              v-model="dataForm.number"
              placeholder="请输入内容"
              clearable
            ></el-input>
        </el-form-item>
          <el-form-item prop="salesman" class="is-required inp"
          ><div>业务员：</div>
          <el-select
              v-model="dataForm.salesman"
              placeholder="业务员请选择"
              clearable
            >
              <el-option
                v-for="item in options1"
                :key="item.value"
                :label="item.label"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item prop="organName" class="is-required inp"
          ><div>客户：</div>
          <el-select
              v-model="dataForm.organName"
              placeholder="客户请选择"
              clearable
            >
              <el-option
                v-for="item in options2"
                :key="item.value"
                :label="item.label"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item class="btn">
            <el-button @click="getDataList()">查询</el-button>
            <el-button @click="resetDataList()">重置</el-button>
          </el-form-item>
        </el-form>
        </div>
        <div class="form1 headForm">
          <h3>销售订单表头</h3>
          <el-table
          :data="dataList"
          border
          v-loading="dataListLoading"
          @row-click="singleElection"
          style="width: 100%"
        >
          <el-table-column label="" width="65">
            <template slot-scope="scope">
              <el-radio
                class="radio"
                v-model="templateSelection"
                :label="scope.$index"
                >&nbsp;</el-radio
              >
            </template>
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            width="50">
          </el-table-column>
          <el-table-column
            prop="operTime"
            header-align="center"
            align="center"
            label="单据日期"
          >
          </el-table-column>
          <el-table-column
            prop="defaultNumber"
            header-align="center"
            align="center"
            label="单据编号"
          >
          </el-table-column>
          <el-table-column
            prop="organName"
            header-align="center"
            align="center"
            label="客户"
          >
          </el-table-column>
          <el-table-column
            prop="userName"
            header-align="center"
            align="center"
            label="业务员"
          >
          </el-table-column>
          <el-table-column
            prop="userName2"
            header-align="center"
            align="center"
            label="制单人"
          >
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="sizeChangeHandle"
          @current-change="currentChangeHandle"
          :current-page="pageIndex"
          :page-size="pageSize"
          :total="totalPage"
          layout="total, prev, pager, next, jumper"
          class="page"
        >
        </el-pagination>
        </div>
        <div class="form1 mainForm" v-if="goodsinfoList.length>0">
          <h3>销售订单表体</h3>
          <el-table
          :data="goodsinfoDataList"
          border
          v-loading="dataListLoading"
          @row-click="singleElection"
          style="width: 100%"
          @selection-change="changegoodsinfoDataList"
        >
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            prop="DepotName"
            header-align="center"
            align="center"
            width="80"
            label="仓库"
          >
          </el-table-column>
          <el-table-column
            prop="mBarCode"
            header-align="center"
            align="center"
            label="货品编码"
          >
          </el-table-column>
          <el-table-column
            prop="name"
            header-align="center"
            align="center"
            label="货品名称"
          >
          </el-table-column>
          <el-table-column
            prop="standard"
            header-align="center"
            align="center"
            width="180"
            label="规格型号"
          >
          </el-table-column>
          <el-table-column
            prop="number"
            header-align="center"
            align="center"
            label="数量"
          >
          </el-table-column>
          <el-table-column
            prop="Unit"
            header-align="center"
            align="center"
            label="计量单位"
          >
          </el-table-column>
          <el-table-column
            prop="availableAmount"
            header-align="center"
            align="center"
            label="可执行数量"
          >
          </el-table-column>
        </el-table>
        </div>
        <p class="btn btn1">
          <span slot="footer" class="dialog-footer">            
            <el-button @click="goodsCancel">取 消</el-button>
            <el-button type="primary" @click="goodsSure">确 定</el-button>
          </span>
        </p>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import axios from "axios";
export default {
  props: ["ischeckForm","istype"], // 父组件给子组件传递值的时候就需要通过props去传递
  data() {
    return {
      dialogVisible: true,
      dataForm: {
        model1: "",
        model2: "",
        category: "",
        barcode: "",
        name: "",
        specifications1: "",
        specifications2: "",
        value: "",
        organName: "",
        salesman: "",
        number: "",
      },
      dataList: [],
      goodsinfoDataList: [],
      options: [],
      options2: [],
      pageIndex: 1,
      pageSize: 5,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      categoryIds: "",
      titleHead: "销售订单查找",
      templateSelection: "",
      goodsinfoSelection: "",
      goodsinfo: {},
      goodsinfoList: [],
      goodsinfoData: [],
      options1: [],
      isCustomer: false,
    };
  },
  created() {
    this.getDataList();
    this.getCategory();
    this.salesman();
    this.getcustomer();
    console.log(this.istype)
    if(this.istype==2){
      this.titleHead="销售出库单查找"
    }
  },
  methods: {
    changegoodsinfoDataList(val){
      console.log(val)
      this.goodsinfoData=val
    },
    getcustomer() {
      let str = `{"supplier":"","type":"客户","phonenum":"","telephone":"","description":""}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/supplier/list?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: 1,
          pageSize: 1000,
        }),
      }).then((data) => {
        console.log(data);
        if (data.data && data.data.code === 0) {
          this.options2 = data.data.data.filter((fitem) => {
            // console.log(fitem)
            fitem.value = fitem.id;
            fitem.label = fitem.supplier;
            return fitem;
          });
        }
      });
    },
    // 获取业务员下拉
    salesman() {
      this.$http({
        url: this.$http.adornUrl(`/depot/getAllUser`),
        method: "get",
      }).then((data) => {
        console.log(data);
        let res = JSON.parse(JSON.stringify(data.data.data));
        res = res.filter((item) => {
          return (item.value = item.user_id), (item.label = item.username);
        });
        this.options1 = res;
      });
    },
    // 确定按钮
    goodsSure() {
      if (typeof this.templateSelection == "string") {
        this.$message({
          message: "请选中订单",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      if(this.goodsinfoData.length==0){
        this.$message({
          message: "请选中商品",
          type: "warning",
          duration: 1500,
        });
        return;
      }
      console.log(this.goodsinfoData)
      console.log(this.templateSelection)
      console.log(this.goodsinfoSelection)
      this.dialogVisible = false;
      console.log(this.goodsinfo);
      this.$emit("getGoods", this.goodsinfo);
      this.$emit("goodsinfo", this.goodsinfoData);
      this.$emit("changecheckForm", this.dialogVisible);
    },
    // 取消按钮
    goodsCancel() {
      // console.log(this.ischeckForm)
      // return
      this.dialogVisible = false;
      this.$emit("changecheckForm", this.dialogVisible);
    },
    // 选中当前行
    singleElection(row) {
      console.log(row.id);
      this.getgoodsinfoDataList(row.id)
      // return;
      this.goodsinfo = row;
      this.goodsinfoList = []
      this.goodsinfoList.push(this.goodsinfo)
      // console.log(this.goodsinfoList.length)
      // this.templateRadio = row.id;
    },
    getgoodsinfoDataList(id){
      this.$http({
          url: this.$http.adornUrl(`/depotItem/getDetailList`),
          method: "get",
          params: this.$http.adornParams(
            {
              headerId: id,
              mpList: "undefined%2Cundefined%2Cundefined",
            },
            false
          ),
        }).then(({ data }) => {
          console.log(data.data.rows)
          let dataList = JSON.parse(JSON.stringify(data.data.rows));
          dataList.map((item) => {
            // this.addRow()
            item.mBarCode = item.MaterialName.split("_")[0];
            item.name = item.MaterialName.split("_")[1].split("(")[0];
            item.standard = item.MaterialName.split("_")[1]
              .split("(")[1]
              .split(")")[0];
            item.number = item.OperNumber;
            item.unit = item.Unit;
            item.tax = item.TaxRate;
            item.lowDecimal = item.UnitPrice;
            item.stock = item.Stock;
            item.remark = item.Remark;
            item.externalNumber = item.externalNumber;
            console.log(item)
          });
          this.goodsinfoDataList=dataList
        })
    },
    // 选中分类获取下拉状态
    handlechange() {
      console.log(this.$refs["refSubCat"].getCheckedNodes()[0].value);
      this.categoryIds = this.$refs["refSubCat"].getCheckedNodes()[0].value;
    },
    // 下拉
    checked(e) {
      if (e) {
        this.getCategory();
      }
    },
    // 选中
    change(id) {
      console.log(id);
      // this.dataForm.id = id;
    },
    // 获取分类的下拉框
    getCategory() {
      this.$http({
        url: this.$http.adornUrl(
          "/materialCategory/getMaterialCategoryTree?id="
        ),
        method: "post",
      }).then(({ data }) => {
        console.log("成功获取到菜单数据...", data.data);
        this.options = this.getTreeData(data.data);
      });
    },
    // 递归判断列表，把最后的children设为undefined
    getTreeData(data) {
      for (var i = 0; i < data.length; i++) {
        if (data[i].children.length < 1) {
          data[i].children = undefined;
        } else {
          this.getTreeData(data[i].children);
        }
      }
      return data;
    },
    // 获取数据列表
    getDataList() {
      console.log(this.dataForm.salesman.username)
      this.dataListLoading = true;
      let type = "其它";
      let subType = "销售订单";
      let roleType = "全部数据";
      if(this.istype==2){
        type = "出库";
        subType = "销售";
      }
      let str = `{"type":"${type}","subType":"${subType}","roleType":"${roleType}","state":"","organName":"${this.dataForm.organName.supplier||""}","number":"${this.dataForm.number}","beginTime":"","endTime":"","materialParam":"","userName":"${this.dataForm.salesman.username||""}","payType":"","serviceType":"","transportType":"","status":"","depotIds":"","deliverDate":"","salesDate":"","billType":"","externalNumber":"","stockType":"","deliveryType":"",deliveryBillNo:"",salesBillNo:""}`;
      var search = encodeURIComponent(str);
      this.$http({
        url: this.$http.adornUrl(`/depotHead/list2?search=${search}`),
        method: "get",
        params: this.$http.adornParams({
          currentPage: this.pageIndex,
          pageSize: this.pageSize,
        }),
      }).then(({ data }) => {
         console.log(data)
        if (data && data.code === 0) {
          this.dataList = data.data.list;
          this.totalPage = data.data.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 更新状态
    updateMemberStatus(data) {
      console.log("最新信息", data);
      let { id, status } = data;
      //发送请求修改状态
      this.$http({
        url: this.$http.adornUrl("/member/member/update/status"),
        method: "post",
        data: this.$http.adornData({ id, status }, false),
      }).then(({ data }) => {
        this.$message({
          type: "success",
          message: "状态更新成功",
        });
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
      this.templateSelection = "";
      this.goodsinfoList = [];
    },
    // 重置
    resetDataList() {
      // console.log(11)
      this.dataForm.barcode = "";
      this.dataForm.number = "";
      this.dataForm.salesman = "";
      this.dataForm.organName = "";
      this.getDataList();
    },
    downLoadFiles(url, params) {
      return new Promise((resolve, reject) => {
        axios
          .post(this.$http.adornUrl(url), params, {
            responseType: "blob",
            headers: {
              "Content-Type": "application/json; application/octet-stream",
            },
          })
          .then((res) => {
            if (!res) {
              Message.warning("导出失败");
              return;
            }
            resolve(res);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
  },
};
</script>
<style scoped>
.el-form-item .el-form-item__content div {
  display: inline-block;
}
.el-input {
  position: relative;
  font-size: 0.072917rem;
  display: inline-block;
  width: 150px;
}
.cancel {
  position: absolute;
  right: 20px;
  top: 18px;
}
.wrap >>> .el-dialog__header {
  padding: 0.104167rem 0.104167rem 0.052083rem;
  background: #E6F5FE;
  text-align: left;
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #31333D;
}
.wrap >>> .el-dialog__title {
  font-size: 16px;
  font-family: Microsoft YaHei;
  font-weight: 400;
  color: #31333D;
}
.wrap >>> .el-dialog__body {
  padding: 20px 40px;
}
.wrap >>> .has-gutter th {
  background: #F0F5FA;
}
.el-button {
  width: 80px;
  height: 32px;
}
.wrap .btn {
  text-align: right;
}
.page {
  text-align: center;
}
.wrap >>> .headInfo .el-range-editor--medium .el-range-separator {
  line-height: 36px;
}
.wrap >>> .el-date-editor{
  width: 180px;
}
.wrap >>> .el-date-editor .el-range-separator {
  width: 20%;
}
.wrap >>> .el-date-editor .el-range-input, .el-date-editor .el-range-separator {
  height: 24px;
}
.wrap >>> .el-date-editor .el-range__icon {
    font-size: 0.072917rem;
    margin-left: -0.026042rem;
    color: #c0c4cc;
    float: left;
    line-height: 0.166667rem;
    width: 0;
    display: none;
}
.wrap >>> .inp .el-input{
  width: 180px;
}
.wrap >>> .date{
  margin-left: 20px;
}
.wrap >>> .inp{
  margin-left: 40px;
}
.wrap >>> .headInfo .btn{
  display: block;
  margin-top: -10px;
  text-align: right;
}
.wrap >>> .headInfo .btn .el-form{
  color: #31333D;
}
.wrap >>> .headInfo .btn .el-button{
  width: 62px;
  height: 30px;
  background: #F2F9FF;
  border: 1px solid #3153C8;
  border-radius: 4px;
  text-align: center;
  padding: 0;
  color: #3153C8;
}
.wrap >>> .headInfo .btn .el-button:first-child{
  color: #fff;
  background: #3153C8;
}
.wrap >>> .headForm h3{
  margin-top:-10px ;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333D;
}
.wrap >>> .headForm .el-table thead {
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333D;
  background: #F0F5FA;
}
.wrap >>> .headForm .el-table td, .el-table th {
    text-align: center;
}
.wrap >>> .mainForm h3{
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333D;
}
.wrap >>> .mainForm .el-table thead {
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: bold;
  color: #31333D;
  background: #F0F5FA;
}
.wrap >>> .form1 .el-table--enable-row-hover .el-table__body tr:hover>td {
    background-color: #F5FAFF;
}
.wrap >>> .btn{
  margin-top: 25px;
  text-align: center;
}
.wrap >>> .btn .el-button{
  width: 140px;
  height: 40px;
  background: #FFFFFF;
  border: 1px solid #3153C8;
  border-radius: 4px;
  color: #3153C8;
}
.wrap >>> .btn1 .el-button:nth-child(2){
  background: #3153C8;
  color: #fff;
  margin-left: 35px;
}
.wrap >>> .mainForm .el-table td,.wrap >>> .mainForm .el-table th{
  text-align: center;
}
</style>
